home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / EX7_9.C < prev    next >
C/C++ Source or Header  |  1992-09-28  |  2KB  |  48 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12.  
  13. #include <cool/Hash_Table.h>            // Include Hash_Table class
  14. #include <cool/Gen_String.h>            // Include COOL String class
  15. #include <cool/Iterator.h>            // Include COOL Iterator class
  16. #include "paragraph.h"                // Include Stroustrup text
  17.  
  18. #include <cool/Hash_Table.C>
  19.  
  20. int main (void) {
  21.   CoolHash_Table<CoolGen_String,int> h1;    // Declare CoolHash_Table variable
  22.   CoolGen_String s;                // Temporary string variable
  23.   int counter = 0, max_count = 0;        // Initialize word counters
  24.   cout << text;                    // Output paragraph
  25.   text.compile ("[a-zA-Z]+");            // Match any alphabetical word
  26.   while (text.find ()) {            // While still more words
  27.     text.sub_string (s, text.start (), text.end ()); // Get word from paragraph
  28.     if (h1.find (s))                      // If word already found
  29.       h1.put (h1.key (), h1.value ()+1);         // Update use count
  30.     else 
  31.       h1.put (s, 1);                // Else add word 
  32.   }
  33.   h1.reset ();                    // Invalidate current position
  34.   CoolIterator<CoolHash_Table<CoolGen_String,int> > i1;;        // CoolIterator object
  35.   while (h1.next ()) {                 // While there are still nodes
  36.     counter += h1.value ();            // Sum number of words used
  37.     if (h1.value () > max_count) {         // If most used word so far
  38.       i1 = h1.current_position ();            // Save position in list
  39.       max_count = h1.value ();                // And keep track of usage
  40.     }
  41.   }
  42.   cout << "There are " << counter << " words\n";
  43.   cout << "There are " << h1.length () << " unique words\n";
  44.   h1.current_position () = i1;            // Set position of most used word
  45.   cout << "The most common word is `" << h1.key () << "' and is used " << h1.value () << " times\n";
  46.   return (0);                    // Exit with successful status
  47. }
  48.